package com.paobuji.aidebao.myapplication.tool;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.paobuji.aidebao.myapplication.appinterface.GetInterfaceObjctListener;
import com.paobuji.aidebao.myapplication.bean.AddLookQuestionResult;
import com.paobuji.aidebao.myapplication.bean.AddQuestionResult;
import com.paobuji.aidebao.myapplication.bean.AddQuestionUsefulResult;
import com.paobuji.aidebao.myapplication.bean.AddSportHistory;
import com.paobuji.aidebao.myapplication.bean.AppVersionResult;
import com.paobuji.aidebao.myapplication.bean.GetAllQuestion;
import com.paobuji.aidebao.myapplication.bean.GetRankingList;
import com.paobuji.aidebao.myapplication.bean.GetSportHistoryList;
import com.paobuji.aidebao.myapplication.bean.SendSmsResult;
import com.paobuji.aidebao.myapplication.bean.UserBean;
import com.paobuji.aidebao.myapplication.bean.UserBeanResult;
import com.paobuji.aidebao.myapplication.model.MyIntentModel;
import com.paobuji.aidebao.myapplication.model.SmsModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by lenovo on 2017/3/13.
 */

public class MyIntentTools {

    private GetInterfaceObjctListener getInterfaceObjctListener;


    //定义接口入住对象
    public void setGetInterfaceObjctListener(GetInterfaceObjctListener getInterfaceObjctListener){
        this.getInterfaceObjctListener = getInterfaceObjctListener;
    }
    ACache aCache;
    UserBean userBean = UserBean.getUserBean();

    /**
     * 用户注册
     * @param context
     */
    public void registUser(final Context context,String phone,String pass,String identified){
        aCache = ACache.get(context);

        Call<UserBeanResult> call = MyIntentModel.getModel().getService().Regist(phone,pass,identified);

        call.enqueue(new Callback<UserBeanResult>() {
            @Override
            public void onResponse(Call<UserBeanResult> call, Response<UserBeanResult> response) {
                if (response.isSuccessful()) {
                    UserBeanResult result = response.body();

                        getInterfaceObjctListener.getObjectDate(result);
                }
            }

            @Override
            public void onFailure(Call<UserBeanResult> call, Throwable t) {

            }
        });
    }

    /**
     * 用户登录
     */
    public void loginUserByPhone(final Context context,String phone,String pass){
        aCache = ACache.get(context);

        Call<UserBeanResult> call = MyIntentModel.getModel().getService().loginByPhone(phone,pass);

        call.enqueue(new Callback<UserBeanResult>() {
            @Override
            public void onResponse(Call<UserBeanResult> call, Response<UserBeanResult> response) {
                if (response.isSuccessful()) {
                    UserBeanResult result = response.body();
                    getInterfaceObjctListener.getObjectDate(result);
                }
            }

            @Override
            public void onFailure(Call<UserBeanResult> call, Throwable t) {

            }
        });
    }

    /**
     * 游客登录
     */
    public void loginUserByIdentified(final Context context,String identified){
        aCache = ACache.get(context);

        Call<UserBeanResult> call = MyIntentModel.getModel().getService().loginByIdentified(identified);

        call.enqueue(new Callback<UserBeanResult>() {
            @Override
            public void onResponse(Call<UserBeanResult> call, Response<UserBeanResult> response) {
                if (response.isSuccessful()) {
                    UserBeanResult result = response.body();

                    if (result.getCode() == 0) {
                        getInterfaceObjctListener.getObjectDate(result);
                    }else{
                        Toast.makeText(context,"登录失败！",Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<UserBeanResult> call, Throwable t) {

            }
        });
    }

    /**
     * 修改用户信息
     */
    public void UpdateUserInfoResult(final Context context,int id,String name,
                                     String usersex,String birt,String userhobby,String userImg,double weight,double height){
        aCache = ACache.get(context);

        Call<UserBeanResult> call = MyIntentModel.getModel().getService().UpdateUserInfoResult(id,name,usersex,birt,userhobby,userImg,weight,height);

        call.enqueue(new Callback<UserBeanResult>() {
            @Override
            public void onResponse(Call<UserBeanResult> call, Response<UserBeanResult> response) {
                if (response.isSuccessful()) {
                    UserBeanResult result = response.body();
                    getInterfaceObjctListener.getObjectDate(result);
                }else{
                    Toast.makeText(context,"保存失败。。。",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<UserBeanResult> call, Throwable t) {

            }
        });
    }


    /**
     * 修改密码
     */
    public void updateUserPass(String userphone,String userpass){

        Call<UserBeanResult> call = MyIntentModel.getModel().getService().updateUserPass(userphone,userpass);

        call.enqueue(new Callback<UserBeanResult>() {
            @Override
            public void onResponse(Call<UserBeanResult> call, Response<UserBeanResult> response) {
                if (response.isSuccessful()) {
                    UserBeanResult result = response.body();
                    getInterfaceObjctListener.getObjectDate(result);
                }else {

                }
            }

            @Override
            public void onFailure(Call<UserBeanResult> call, Throwable t) {

            }
        });
    }

    /**
     * 根据电话查询用户信息
     */
    public void selectUserByPhone(final Context context,String userphone){
        Call<UserBeanResult> call = MyIntentModel.getModel().getService().selectUserByPhone(userphone);

        call.enqueue(new Callback<UserBeanResult>() {
            @Override
            public void onResponse(Call<UserBeanResult> call, Response<UserBeanResult> response) {
                if (response.isSuccessful()) {
                    UserBeanResult result = response.body();
                    getInterfaceObjctListener.getObjectDate(result);
                }
            }

            @Override
            public void onFailure(Call<UserBeanResult> call, Throwable t) {

            }
        });
    }


    /**
     * 添加一条运动记录
     */
    public void addSportHistory(final Context context,int userId,int sportTime,double alldis,double allCor){
        Call<AddSportHistory> call = MyIntentModel.getModel().getService().addSportHistory(userId,sportTime,alldis,allCor);

        call.enqueue(new Callback<AddSportHistory>() {
            @Override
            public void onResponse(Call<AddSportHistory> call, Response<AddSportHistory> response) {
                if (response.isSuccessful()) {
                    AddSportHistory result = response.body();
                    getInterfaceObjctListener.getObjectDate(result);
                }
            }

            @Override
            public void onFailure(Call<AddSportHistory> call, Throwable t) {

            }
        });
    }


    /**
     * 查询某人的运动记录
     */
    public void getSportHitoryList(final Context context,int userId){

        Call<GetSportHistoryList> call = MyIntentModel.getModel().getService().getSportHitoryList(userId);

        call.enqueue(new Callback<GetSportHistoryList>() {
            @Override
            public void onResponse(Call<GetSportHistoryList> call, Response<GetSportHistoryList> response) {
                if (response.isSuccessful()) {
                    GetSportHistoryList result = response.body();
                    getInterfaceObjctListener.getObjectDate(result);
                }
            }

            @Override
            public void onFailure(Call<GetSportHistoryList> call, Throwable t) {

            }
        });
    }


    /**
     * 查询排行榜
     */
    public void getRankingList(final Context context,int page,int size){
        Call<GetRankingList> call = MyIntentModel.getModel().getService().getRankingList(page,size);

        call.enqueue(new Callback<GetRankingList>() {
            @Override
            public void onResponse(Call<GetRankingList> call, Response<GetRankingList> response) {
                if (response.isSuccessful()) {
                    GetRankingList result = response.body();

                    getInterfaceObjctListener.getObjectDate(result);
                }
            }

            @Override
            public void onFailure(Call<GetRankingList> call, Throwable t) {

            }
        });
    }

    /**
     * 添加一个问题
     */
    public void addQuestion(final Context context,String quesTitle,String quesType,String quesAnswer,String keyWord){
        aCache = ACache.get(context);

        Call<AddQuestionResult> call = MyIntentModel.getModel().getService().addQuestion(quesTitle,quesType,quesAnswer,keyWord);

        call.enqueue(new Callback<AddQuestionResult>() {
            @Override
            public void onResponse(Call<AddQuestionResult> call, Response<AddQuestionResult> response) {
                if (response.isSuccessful()) {
                    AddQuestionResult result = response.body();

                    if (result.getCode() == 0) {
                        getInterfaceObjctListener.getObjectDate(result);
                    }else{
                        Toast.makeText(context,"添加失败！",Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<AddQuestionResult> call, Throwable t) {

            }
        });
    }


    /**
     * 统计某个问题的总的浏览人数
     */
    public void addLookQuestionNum(final Context context,int id){
        aCache = ACache.get(context);

        Call<AddLookQuestionResult> call = MyIntentModel.getModel().getService().addLookQuestionNum(id);

        call.enqueue(new Callback<AddLookQuestionResult>() {
            @Override
            public void onResponse(Call<AddLookQuestionResult> call, Response<AddLookQuestionResult> response) {
                if (response.isSuccessful()) {
                    AddLookQuestionResult result = response.body();

                    if (result.getCode() == 0) {
                        getInterfaceObjctListener.getObjectDate(result);
                    }else{
                        Toast.makeText(context,"统计失败！",Toast.LENGTH_SHORT).show();
                    }
                }
            }
            @Override
            public void onFailure(Call<AddLookQuestionResult> call, Throwable t) {

            }
        });
    }

    /**
     * 统计某个问题的回答点赞
     */
    public void addQuestionUseful(final Context context,int id){

        Call<AddQuestionUsefulResult> call = MyIntentModel.getModel().getService().addQuestionUseful(id);

        call.enqueue(new Callback<AddQuestionUsefulResult>() {
            @Override
            public void onResponse(Call<AddQuestionUsefulResult> call, Response<AddQuestionUsefulResult> response) {
                if (response.isSuccessful()) {
                    AddQuestionUsefulResult result = response.body();
                        getInterfaceObjctListener.getObjectDate(result);
                }
            }

            @Override
            public void onFailure(Call<AddQuestionUsefulResult> call, Throwable t) {

            }
        });
    }



    /**
     * 获取问题列表
     */
    public void getAllQuestionList(final Context context,String keyWord,int page,int size){
        Call<GetAllQuestion> call = MyIntentModel.getModel().getService().getAllQuestionList(keyWord,page,size);

        call.enqueue(new Callback<GetAllQuestion>() {
            @Override
            public void onResponse(Call<GetAllQuestion> call, Response<GetAllQuestion> response) {
                if (response.isSuccessful()) {
                    GetAllQuestion result = response.body();
                    Gson gson = new Gson();
                    Log.e("11111", "onResponse: "+gson.toJson(result ));
                    getInterfaceObjctListener.getObjectDate(result);
                }
            }

            @Override
            public void onFailure(Call<GetAllQuestion> call, Throwable t) {

            }
        });
    }


    /**
     * 发送短信验证码
     */
    public void SendSms(final Context context,String action,String account,String password,String userid,String mobile,String content,String rt){

        Call<SendSmsResult> call = SmsModel.getModel().getService().sendSms(action,account,password,userid,mobile,content,rt);

        call.enqueue(new Callback<SendSmsResult>() {
            @Override
            public void onResponse(Call<SendSmsResult> call, Response<SendSmsResult> response) {
                if (response.isSuccessful()) {
                    SendSmsResult result = response.body();

                    //Log.i("jianbaopp",result.toString());

                    if ("ok".equals(result.getMessage())) {
                        //Toast.makeText(RegistActivity.this,result.getSuccessCounts(),Toast.LENGTH_SHORT).show();
                        getInterfaceObjctListener.getObjectDate(result);

                    }else{
                        //Toast.makeText(RegistActivity.this,result.getSuccessCounts()+"",Toast.LENGTH_SHORT).show();
                        Toast.makeText(context,"验证码获取失败！" + result.getMessage(),Toast.LENGTH_SHORT).show();

                    }
                }else{
                    Toast.makeText(context,"验证码获取失败000！",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<SendSmsResult> call, Throwable t) {
               // Log.i("jianbaopp",t.getMessage());
                Toast.makeText(context,t.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });
    }


    /**
     * 统计某个问题的总的浏览人数
     */
    public void getAppVersion(int id){

        Call<AppVersionResult> call = MyIntentModel.getModel().getService().getAppVersion(id);

        call.enqueue(new Callback<AppVersionResult>() {
            @Override
            public void onResponse(Call<AppVersionResult> call, Response<AppVersionResult> response) {
                if (response.isSuccessful()) {
                    AppVersionResult result = response.body();
                    getInterfaceObjctListener.getObjectDate(result);
                }
            }

            @Override
            public void onFailure(Call<AppVersionResult> call, Throwable t) {

            }
        });
    }
}
